﻿using Echo.Context.UltiProNet.Contexts;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by:Yesenia Sosa
////Date created:10/30/2012
////Modified by: 
////Date modified: 
////Reason: 
////ULTI-86091  2012 PR W-2: Add new flag for Fringe benefits reported on PR W-2 - .NET
////ULTI-86094  2012 PR W-2: Add label for Fringe benefits reported on PR W-2 - .NET Summary Page
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules
{
    [TestFixture]
    [Header("Config")]
    [Config(".NET")]
    [Author("Yesenia Sosa", "yesenia_sosa@ultimatesoftware.com")]
    [Category("Deductions")]
    [Category("Business Rules")]
    [Category("PR W-2")]
    public class PRW2NonTaxableFringe : MbUnitAutomatedTest
    {
        #region Setup
        UltiPro UltiPro;
        UltiProDatabaseContext Database;
        private const string FringeBenefits = "Fringe benefits reported on PR W-2";

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            #region Given that UltiPro is launched and I am logged in as MerrillG
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            UltiPro.Pages.Login.LoginAs("merrillg", "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("CALEN", "C0014");
            #endregion

            #region And I navigate to the deduction setup via business rules
            UltiPro.Menu.SystemConfiguration.Click(x => x.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(row => row.BusinessRulesNameFilter, FilterType.StartsWith, "Deduction/Benefit Plans");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(row => row.BusinessRulesLink, "Deduction/Benefit Plans").Click(link => link.BusinessRulesLink);
            #endregion
        }        

        [SetUp]
        public void Setup()
        {
            Database.Company.Execute("update DedCode set DedPRW2NonTaxableFringe = 'N' where dedDedCode = 'BUXEX'");
            UltiPro.Browser.SwitchToMainWindow();
        }
        #endregion

        #region TearDown
        [TearDown]
        public void TearDown()
        {
            Database.Company.Execute("update DedCode set DedPRW2NonTaxableFringe = null where dedDedCode = 'BUXEX'");
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            UltiPro.Browser.Quit();
        }
        #endregion

        public void NavigateToPayrollPageOfWizard()
        {
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableDeductionPlansGrid.Set(x => x.SearchTextBox, "BUXEX");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableDeductionPlansGrid.Row(row => row.CodeLink, "BUXEX").Click(link => link.CodeLink);
            UltiPro.Pages.BusinessRuleDetail.Click(x => x.EditPlanNextButton);
            UltiPro.Browser.SwitchToWindow("Deduction Setup");
            UltiPro.Pages.DeductionPlanSummary.ButtonBar.Click(btn => btn.Next);
            UltiPro.Pages.DeductionPlanCalculations.ButtonBar.Click(btn => btn.Next);
        }

        [Test]
        public void VerifyFieldSavesToYes()
        {
            #region Given I navigate to the Payroll page of the wizard
                NavigateToPayrollPageOfWizard();
            #endregion

            #region When the payroll screen loads, I select the PRW2NonTaxableFringe check box and save
            UltiPro.Pages.DeductionPlanPayroll.Is(lbl => lbl.FringeBenefitsReportedOnPRW2Label, "innerHtml", FringeBenefits);
            UltiPro.Pages.DeductionPlanPayroll.NotSelected(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.Click(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.ButtonBar.Click(btn => btn.Next);
            UltiPro.Pages.DeductionSetupSubmitProcess.ButtonBar.Click(btn => btn.Save);
            #endregion

            #region Then I verify sql for value of Y
            Database.Company.Verify("select DedPRW2NonTaxableFringe from dedcode where deddedcode = 'BUXEX'", "PRW2NonTaxableFringe_1.xml");
            #endregion
        }

        [Test]
        public void VerifyFieldSavesToNo()
        {
            #region Given I navigate to the Payroll page of the wizard
            Database.Company.Execute("update DedCode set DedPRW2NonTaxableFringe = 'Y' where dedDedCode = 'BUXEX'");
            NavigateToPayrollPageOfWizard();
            #endregion

            #region When the payroll screen loads, I unselect the PRW2NonTaxableFringe check box and save
            UltiPro.Pages.DeductionPlanPayroll.Is(lbl => lbl.FringeBenefitsReportedOnPRW2Label, "innerHtml", FringeBenefits);
            UltiPro.Pages.DeductionPlanPayroll.Selected(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.Click(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.NotSelected(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.ButtonBar.Click(btn => btn.Next);
            UltiPro.Pages.DeductionSetupSubmitProcess.ButtonBar.Click(btn => btn.Save);
            #endregion

            #region Then I verify sql for value of N
            Database.Company.Verify("select DedPRW2NonTaxableFringe from dedcode where deddedcode = 'BUXEX'", "PRW2NonTaxableFringe_2.xml");
            #endregion
        }

        [Test]
        public void VerifyNoYesSummaryPage()
        {
            #region Given I navigate to the Payroll page of the wizard
            NavigateToPayrollPageOfWizard();
            #endregion

            #region When the payroll screen loads, I select the PRW2NonTaxableFringe check box                        
            UltiPro.Pages.DeductionPlanPayroll.Click(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.Selected(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.ButtonBar.Click(btn => btn.Next);            
            #endregion

            #region Then I verify the Summary page to show NO in Before and YES in After  
            UltiPro.Pages.DeductionSetupSubmitProcess.PayrollGrid.Row(row => row.Label, FringeBenefits, row => row.Before, "No", row => row.After, "Yes").RowExists();
            UltiPro.Pages.DeductionSetupSubmitProcess.ButtonBar.Click(btn => btn.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }

        [Test]
        public void VerifyYesNoSummaryPage()
        {
            #region Given I navigate to the Payroll page of the wizard and the field is set to YES
            Database.Company.Execute("update DedCode set DedPRW2NonTaxableFringe = 'Y' where dedDedCode = 'BUXEX'");
            NavigateToPayrollPageOfWizard();
            #endregion

            #region When the payroll screen loads, I unselect the PRW2NonTaxableFringe check box
            UltiPro.Pages.DeductionPlanPayroll.Click(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.NotSelected(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.ButtonBar.Click(btn => btn.Next); 
            #endregion

            #region Then I verify the Summary page to show YES in Before and NO in After
            UltiPro.Pages.DeductionSetupSubmitProcess.PayrollGrid.Row(row => row.Label, FringeBenefits, row => row.Before, "Yes", row => row.After, "No").RowExists();
            UltiPro.Pages.DeductionSetupSubmitProcess.ButtonBar.Click(btn => btn.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }

        [Test]
        public void VerifyNoNoSummaryPage()
        {
            #region Given I navigate to the Payroll page of the wizard and the field is set to NO  
            NavigateToPayrollPageOfWizard();
            #endregion

            #region When the payroll screen loads, I leave the check box alone
            UltiPro.Pages.DeductionPlanPayroll.NotSelected(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.ButtonBar.Click(btn => btn.Next);
            #endregion

            #region Then I verify the Summary page to show NO in Before and NO in After
            UltiPro.Pages.DeductionSetupSubmitProcess.PayrollGrid.Row(row => row.Label, FringeBenefits, row => row.Before, "No", row => row.After, "No").RowExists();
            UltiPro.Pages.DeductionSetupSubmitProcess.ButtonBar.Click(btn => btn.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }

        [Test]
        public void VerifyYesYesSummaryPage()
        {
            #region Given I navigate to the Payroll page of the wizard and the field is set to YES
            Database.Company.Execute("update DedCode set DedPRW2NonTaxableFringe = 'Y' where dedDedCode = 'BUXEX'");
            NavigateToPayrollPageOfWizard();
            #endregion

            #region When the payroll screen loads, I leave the check box alone
            UltiPro.Pages.DeductionPlanPayroll.Selected(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.ButtonBar.Click(btn => btn.Next);
            #endregion

            #region Then I verify the Summary page to show YES in Before and YES in After
            UltiPro.Pages.DeductionSetupSubmitProcess.PayrollGrid.Row(row => row.Label, FringeBenefits, row => row.Before, "Yes", row => row.After, "Yes").RowExists();
            UltiPro.Pages.DeductionSetupSubmitProcess.ButtonBar.Click(btn => btn.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }

        [Test]
        public void VerifyNoFieldForCanada()
        {
            #region Given I select Canada deduction CTPD
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableDeductionPlansGrid.Set(x => x.SearchTextBox, "CTPD");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableDeductionPlansGrid.Row(row => row.CodeLink, "CTPD").Click(link => link.CodeLink);
            UltiPro.Pages.BusinessRuleDetail.Click(x => x.EditPlanNextButton);
            UltiPro.Browser.SwitchToWindow("Deduction Setup");
            #endregion

            #region When I navigate to the payroll screen
            UltiPro.Pages.DeductionPlanSummary.ButtonBar.Click(btn => btn.Next);
            UltiPro.Pages.DeductionPlanTax.ButtonBar.Click(btn => btn.Next);
            UltiPro.Pages.DeductionPlanCalculations.ButtonBar.Click(btn => btn.Next);
            #endregion

            #region Then the checkbox is not visible
            UltiPro.Pages.DeductionPlanPayroll.NotVisibleOrNotExists(cbx => cbx.FringeBenefitsReportedOnPRW2CheckBox);
            UltiPro.Pages.DeductionPlanPayroll.NotVisibleOrNotExists(lbl => lbl.FringeBenefitsReportedOnPRW2Label);
            UltiPro.Pages.DeductionSetupSubmitProcess.ButtonBar.Click(btn => btn.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }
    }
}
